package com.zjitc.controller;

import com.zjitc.common.Constant;
import com.zjitc.common.ServerResponse;
import com.zjitc.common.ServerResponseCode;
import com.zjitc.pojo.User;
import com.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import redis.clients.jedis.Jedis;

/**
 * Created by lenovo on 2018/4/1.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    @ResponseBody
    public ServerResponse test() {
        User user = new User();
        user.setId(1);
        user.setUsername("hh");
        user.setPassword("pishf");
        user.setEmail("aa@mail.com");
        user.setQuestion("hello");
        user.setAnswer("word");
        user.setRole(0);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        return ServerResponse.createSuccess(user);
    }

    @RequestMapping("/login")
    @ResponseBody
    public ServerResponse login(
            @RequestParam(name = "username") String username,
            @RequestParam(name = "password") String password,
            HttpSession session) {
        ServerResponse<User> response = userService.login(username, password);
        if (response.success()) {
            session.setAttribute("user", response.getData());
        }
        return response;
    }

    @RequestMapping("/register")
    @ResponseBody
    public ServerResponse register(User user) {
        ServerResponse userResponse = userService.register(user);
        return userResponse;
    }

    @RequestMapping("/check_vaild")
    @ResponseBody
    public ServerResponse checkVaild(String str, String type) {
        ServerResponse userResponse = userService.checkVaild(str, type);
        return userResponse;
    }

    @RequestMapping("/get_user_info")
    @ResponseBody
    public ServerResponse getUserInfo(HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        if (user != null) {
            return ServerResponse.createSuccess(user);
        }
        return ServerResponse.createError("用户未登录，无法获取当前用户信息");
    }

    @RequestMapping("/forget_get_question")
    @ResponseBody
    public ServerResponse forgetGetQuestion(String username) {
        ServerResponse userResponse = userService.selectQuestions(username);
        return userResponse;
    }

    @RequestMapping(value = "/forget_check_answer")
    @ResponseBody
    public ServerResponse<String> forgetCheckAnswer(String username, String question, String answer) {
        ServerResponse userResponse = userService.checkAnswer(username, question, answer);
        return userResponse;
    }

    @RequestMapping(value = "/forget_reset_password")
    @ResponseBody
    public ServerResponse<String> forgetResetPassword(String username, String passwordNew, String forgetToken) {
        ServerResponse userResponse = userService.forgetResetPassword(username, passwordNew, forgetToken);
        return userResponse;
    }

    @RequestMapping("/reset_password")
    @ResponseBody
    public ServerResponse<String> resetPassword(HttpSession session, String passwordOld, String passwordNew) {
        User user = (User) session.getAttribute(Constant.USER);
        if (user == null) {
            return ServerResponse.createError("用户未登录");
        }
        ServerResponse userResponse = userService.resetPassword(passwordOld, passwordNew, user);
        return userResponse;
    }

    @RequestMapping("/update_information")
    @ResponseBody
    public ServerResponse<User> updateInformation(HttpSession session, User user) {
        User currentUser = (User) session.getAttribute(Constant.USER);
        if (currentUser == null) {
            return ServerResponse.createError("用户未登录");
        }
        user.setId(currentUser.getId());
        user.setUsername(currentUser.getUsername());
        ServerResponse<User> response = userService.updateInformation(user);
        if (response.success()) {
            response.getData().setUsername(currentUser.getUsername());
            session.setAttribute(Constant.USER, response.getData());
        }
        return response;
    }

    @RequestMapping("/get_information")
    @ResponseBody
    public ServerResponse<User> getInformation(HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ServerResponseCode.LOGIN, "用户未登录,无法获取当前用户信息,status=10,强制登录");
        }
        ServerResponse userResponse = userService.getInformation(user.getId());
        return userResponse;
    }
    @RequestMapping("/logout")
    @ResponseBody
    public ServerResponse<String> logout(HttpSession session){
        session.removeAttribute(Constant.USER);
        return ServerResponse.createBySuccess();
    }
}
